<?php

namespace backend\controllers;

use backend\models\file\AttExtend;
use backend\utils\AppUtil;
use backend\utils\CsvUtil;
use backend\utils\FileUpUtil;
use common\extensions\pdf\Pdf;
use common\models\User;
use League\Csv\Reader;
use League\Csv\Writer;
use Yii;
use yii\helpers\Url;

class FileController extends BaseController
{
    /**
     * @desc 下载doc
     * @return void
     * @Date: 2024/10/31
     */
    public function actionDownDoc()
    {
        $EnterpriseInfo = '{"uuid":"b50f7077-fdd6-329f-9d69-687a5bf31c47","batch_id":"1","users_id":"1c5fa85e-3191-370d-9860-63f46d346d33","name":"中国船舶集团有限公司综合技术经济研究院","credit_code":"121000004000004721","competent_department":"2","applicants_name":"于泓涵","applicants_mobile":"18600490347","applicants_email":"yuhonghan@namaedu.com","legal_representative":"曹友生","regtime":"1983-10-01","register_place":"北京市","register_office":"国家事业单位登记管理局","registered_fund":"3187","industry_category":"1,2,5,7,11,12","numbers":"534","scale":"1","type":"209","sales":"40061","total_assets":"7995","bank_credit_rating":"未评级","asset_liability_ratio":"34.13","contact_person":"付潇","duty":"无","telephone":"16619992261","email":"school@namaedu.com","basic_info":"中国船舶集团有限公司综合技术经济研究院（简称“中船综合院”）隶属于中国船舶集团有限公司...","titles":"1,2,3,12,14","other_titles":null,"main_product":"1000台套左右","income":"40061","create_time":"2022-11-18 09:18:55","update_time":"2023-04-19 18:23:38"}';

        $EnterpriseInfo = json_decode($EnterpriseInfo, 1);
        $res = $this->renderPartial('file_doc', [
            'EnterpriseInfo' => $EnterpriseInfo,
        ]);
        Yii::$app->response->sendContentAsFile($res, '中国船舶集团有限公司综合技术经济研究院.doc');
    }

    /**
     * @desc 下载excel
     * @return void
     * @Date: 2024/10/31
     */
    public function actionDownExcel()
    {
        $title = [
            "省份", "申报学校", "项目名称", "服务产业",
            "学校是否国家双高计划建设单位", "学校是否省级双高/优质院校",
        ];

        $excel_data = [
            ['北京', '申报学校1', '项目名称1', '服务产业1', '是', '是'],
            ['河北省', '申报学校2', '项目名称2', '服务产业2', '否', '否'],
        ];

        AppUtil::pre_export_excel('省厅推荐项目', $title, $excel_data);
        exit;
    }

    /**
     * @desc 下载pdf
     * @return void
     * @Date: 2024/10/31
     */
    public function actionDownPdf()
    {
        ini_set('max_execution_time', '0');
        ini_set('memory_limit', '-1');

        $pdf = new Pdf('L', 'mm', 'A4');
        $pdf->AddGBFont();
        $pdf->SetFont('GB');
        $pdf->SetMargins(15, 18);

        $pdf->title = '河北省2024年中职拟招生专业汇总表';

        $data = [
            ['学校名称1', '专业代码', '专业名称', '学制', '拟招生人数', '备注'],
            ['学校名称2', '专业代码', '专业名称', '学制', '拟招生人数', '备注'],
        ];

        $pdf->AddPage();
        $pdf->SetFontSize(18);
        $pdf->Cell(0, 10, iconv('utf-8', 'gbk', $pdf->title), 0, 0, 'C');
        $pdf->Ln(18);
        $pdf->SetFontSize(12);
        $pdf->Cell(40, 10, iconv('utf-8', 'gbk', '主管部门：'), 0, 0, 'L');
        $pdf->Cell(40, 10, iconv('utf-8', 'gbk', '(盖章)'), 0, 0, 'R');
        $pdf->Cell(0, 10, iconv('utf-8', 'gbk', '填表日期：' . date('Y-m-d')), 0, 0, 'R');
        $pdf->Ln(13);
        $pdf->SetWidths(array(15, 80, 40, 50, 25, 40, 20));
        $header = array('序号', '学校名称', '专业代码', '专业名称', '学制', '拟招生人数', '备注');
        $pdf->Row($header);

        foreach ($data as $k => $info) {
            $row = [
                $k + 1, // 序号
                $info[0],
                $info[1],//专业代码
                $info[2],
                $info[3],
                $info[4],//拟招生人数
                $info[5],//备注

            ];
            $pdf->Row($row);
        }

        $pdf->Output($pdf->title . '.pdf', 'D');
    }

    /**
     * @desc HtmlUtils::html_a_down() 方法有调用
     * @return void
     * @Date: 2024/11/1
     */
    public function actionDownFile()
    {
        $id = Yii::$app->request->get('id');

        $model = AttExtend::findOne($id);

        AppUtil::download_file($model->att_position, $model->file_name);
    }

    /**
     * @desc 下载压缩包
     * @return void
     * @Date: 2024/11/1
     */
    public function actionDownZip()
    {
        ini_set('max_execution_time', '0');
        ini_set('memory_limit', '-1');

        $zip_name = "绩效佐证材料";
        $file_arr = [];

        $files = AttExtend::find()->where([])->asArray()->limit(50)->all();
        foreach ($files as $file) {
            $att_position = $file['att_position'];
            $table_field = $file['table_field'];
            $file_name = FileUpUtil::getFileName2($file['file_name']);
            $file_path = AppUtil::get_web_path() . $att_position;

            $table_name = $table_field ? explode('.', $table_field)[0] : '';
            if (file_exists($file_path)) {
                $file_arr[] = ['file_path' => $file_path, 'zip_path' => "{$table_name}/{$file_name}"];
            }
        }

        AppUtil::zip_download_file($zip_name, $file_arr);
        exit;
    }


    /**
     * @desc 生成csv文件
     * @return void
     * @Date: 2024/11/4
     */
    public function actionDownCsv()
    {
        $fileName = "历年实际招生专业";
        $headArr = ['学校名称', '专业名称', '专业代码', '专业大类', '专业类', '招生年份', '实际招生人数', '在校生人数', '毕业生人数', '流失生人数', '状态'];

        $excel_data[] = ['学校名称 1', '专业名称 1', '专业代码 1', '专业大类 1', '专业类 1', '2024年', '12', '23', '33', '6', '??'];
        $excel_data[] = ['学校名称 2', '专业名称 2', '专业代码 2', '专业大类 2', '专业类 2', '2023年', '12', '23', '33', '6', '??'];

        CsvUtil::create_csv_and_down1($fileName, $headArr, $excel_data);
        exit;
    }

    /**
     * @desc 插件地址 https://packagist.org/packages/league/csv#9.8.0
     * @return void
     * @Date: 2024/11/4
     */
    public function actionDownCsv2()
    {
        $header = ['first name', 'last name', 'email'];
        $records = [
            [1, 2, 3],
            ['foo', 'bar', 'baz'],
            ['john', 'doe', 'john.doe@example.com'],
        ];

        //load the CSV document from a string
        $csv = Writer::createFromString();

        //insert the header
        $csv->insertOne($header);

        //insert all the records
        $csv->insertAll($records);

        echo $csv->toString(); //returns the CSV document as a string
    }
}
